package online.flowerinsnow.base64;

import java.nio.charset.StandardCharsets;

public class Base64 {
    public static void main(String[] args) {
        if (args.length > 0) {
            StringBuilder sb = new StringBuilder();
            for (int i = 1; i < args.length; i++) {
                sb.append(args[i]);
                if (i + 1 < args.length) sb.append(" ");
            }

            switch (args[0]) {
                case "-e":
                    System.out.println(java.util.Base64.getEncoder().encodeToString(sb.toString().getBytes(StandardCharsets.UTF_8)));
                    break;
                case "-d":
                    System.out.println(new String(java.util.Base64.getDecoder().decode(sb.toString())));
                    break;
                default:
                    printUsage();
            }
        } else printUsage();
    }

    private static void printUsage() {
        System.out.println("用法: base64 <类型> <数值>\n" +
                "\t类型：\n" +
                "\t\t-e：加密\n" +
                "\t\t-d: 解密");
    }
}
